package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishFlavormMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealDishMapper;

import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavormMapper dishFlavormMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    //新增菜品
    @Transactional
    @Override
    @CacheEvict(value = "Dish_getByCategoryId",allEntries=true)
    public Result addDish(DishDTO dishDTO) {
        if (dishDTO==null){
            return Result.error(MessageConstant.OBJECT_NULL);
        }

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        Dish byName = dishMapper.getByName(dish);
        if (byName!=null){
            return Result.error(MessageConstant.ALREADY_EXISTS);
        }

        dishMapper.addDish(dish);
        //  -- 口味 --
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //没有添加口味，直接返回
        if (flavors.size()==0){
            return Result.success();
        }
        Long id = dish.getId();
        flavors.stream().forEach(flavor->{
            flavor.setDishId(id);
        });

        dishFlavormMapper.addDishFlavorm(flavors);

        return Result.success();
    }

    //分页查询
    @Override

    public Result page(DishPageQueryDTO pageQueryDTO) {
        if (pageQueryDTO.getPage() ==null ){
            pageQueryDTO.setPage(1);

        }
        if (pageQueryDTO.getPageSize() ==null){
            pageQueryDTO.setPageSize(5);
        }
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());

        Page<Dish> page = dishMapper.page(pageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return Result.success(pageResult);
    }

    @Transactional
    @Override
    @CacheEvict(value = "Dish_getByCategoryId",allEntries=true)
    public Result DropByIds(List<Long> ids) {
        Integer count=0;
        for (int i=ids.size()-1;i>=0;i--){
            SetmealDish setmealDish1 = setmealDishMapper.GetSetmealDishByDishId(ids.get(i));
            if (setmealDish1!=null){
                ids.remove(i);
                count++;
            }
        }
        if (ids.size()>0){
            dishMapper.DropByIds(ids);
            dishFlavormMapper.DropDishFlavormByDishId(ids);
            setmealDishMapper.DropByDishId(ids);
        }
        if (count ==0){
            return Result.success();
        }

        return Result.error("有"+count+"条数据无法删除");
    }

    @Override

    public Result GetById(Long id) {
        Dish dish = dishMapper.GetById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);

        return Result.success(dishVO);
    }

    @Override
    @CacheEvict(value = "Dish_getByCategoryId",allEntries=true)
    public Result Update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        try {
            dishMapper.Update(dish);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success();
    }

    @Override

    public Result getByCategoryId(Long categoryId) {
        List<DishVO> byCategoryIds = dishMapper.getByCategoryId(categoryId);
        for (DishVO dishVO : byCategoryIds) {
            List<DishFlavor> byDishId = dishFlavormMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(byDishId);
        }
        return Result.success(byCategoryIds);
    }

}
